import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ViewUI from 'view-design'
import '../them/index.less'
import hljs from 'highlight.js'
import 'highlight.js/styles/vs2015.css'
import marked from 'marked'
import './assets/icon/iconfont.css'

// 挂载到vue原型链上
// Vue.prototype.$author = '至简'

var xhr = new XMLHttpRequest()
xhr.open('get', '/config.json', false)
xhr.send()

Vue.prototype.$config = JSON.parse(xhr.responseText)
Vue.config.productionTip = false
Vue.prototype.$api = require('./utils/api').default
Vue.prototype.$marked = marked
Vue.directive('highlight', el => {
  let blocks = el.querySelectorAll('pre code')
  blocks.forEach(block => {
    hljs.highlightBlock(block)
  })
})
Vue.use(ViewUI)

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
router.beforeEach((to, from, next) => {
  ViewUI.LoadingBar.start()

  /*路由发生改变修改页面的title */
  debugger
  if (to.matched.length === 0) {
    //如果未匹配到路由
    next('/notFound') //如果上级也未匹配到路由则跳转主页面，如果上级能匹配到则转上级路由
  } else {
    if (to.meta.title) {
      document.title = to.meta.title
    }
    if (from.meta.keepAlive == true) {
      store.commit('RecordScrollTop', document.documentElement.scrollTop)
    }
    next() //如果匹配到正确跳转
  }
})
Vue.prototype.$Message.config({
  top: 80,
  duration: 3
})
router.afterEach(to => {
  ViewUI.LoadingBar.finish()
  if (to.hash === '') {
    if (to.meta.keepAlive == false) {
      setTimeout(() => {
        document.documentElement.scrollTop = 0
      }, 10)
    } else {
      setTimeout(() => {
        document.documentElement.scrollTop = store.state.scrollTop
      }, 50)
    }
  }
})
